﻿using IFRS.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace IFRS.DataAccess.DAL
{
    public class SPR_CUSTOMER
    {
        public DataTable SEARCH_ALL(Customer customer, Paging paging, bool IFRS_ALLBRANCH, string sIFRS_COST_CENTER, ref int iRecordCount)
        {
            DataTable dt = new DataTable();
            IFRS_Utility Utility = new IFRS_Utility();
            List<SqlParameter> lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@RecordCount", SqlDbType.Int));
                lstParams[0].Direction = ParameterDirection.Output;
                lstParams.Add(new SqlParameter("@PageIndex", paging.PageIndex));
                lstParams.Add(new SqlParameter("@PageSize", paging.PageSize));
                lstParams.Add(new SqlParameter("@Sorting", paging.Sorting));
                lstParams.Add(new SqlParameter("@isAllBranch", IFRS_ALLBRANCH));
                lstParams.Add(new SqlParameter("@sCostCenter", sIFRS_COST_CENTER));
                lstParams.Add(new SqlParameter("@LicenseNo", customer.LicenseNo.AsDBValue()));
                lstParams.Add(new SqlParameter("@CustomerType", customer.CustomerType.AsDBValue()));
                lstParams.Add(new SqlParameter("@OccupationType", customer.OccupationType.AsDBValue()));
                lstParams.Add(new SqlParameter("@ResidentArea", customer.ResidentArea.AsDBValue()));
                lstParams.Add(new SqlParameter("@StockExchangeRegister", customer.StockExchangeRegister.AsDBValue()));
                dt = Utility.StoredProcDataTable("SPR_CUSTOMER_SEARCHALL", lstParams.ToArray());

                iRecordCount = (int)lstParams[0].Value;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return dt;
        }

        public DataTable SEARCH_BYID(int CustomerID)
        {
            DataTable dt = new DataTable();
            IFRS_Utility Utility = new IFRS_Utility();
            List<SqlParameter> lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@CustomerID", CustomerID));
                dt = Utility.StoredProcDataTable("SPR_CUSTOMER_SEARCHBYID", lstParams.ToArray());
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return dt;
        }

        public int INSERT(Customer customer)
        {
            int iNum = 0;
            IFRS_Utility Utility = new IFRS_Utility();
            List<SqlParameter> lstParams = new List<SqlParameter>();

            try
            {
                lstParams.Add(new SqlParameter("@CustomerID", SqlDbType.Int));
                lstParams[0].Direction = ParameterDirection.Output;
                lstParams.Add(new SqlParameter("@SourceName", customer.SourceName.AsDBValue()));
                lstParams.Add(new SqlParameter("@CustomerCode", customer.CustomerCode.AsDBValue()));
                lstParams.Add(new SqlParameter("@CustomerType", customer.CustomerType.AsDBValue()));
                lstParams.Add(new SqlParameter("@LicenseNo", customer.LicenseNo.AsDBValue()));
                lstParams.Add(new SqlParameter("@PassportNo", customer.PassportNo.AsDBValue()));
                lstParams.Add(new SqlParameter("@IssueAmount", customer.IssueAmount.AsDBValue()));
                lstParams.Add(new SqlParameter("@StockExchangeRegister", customer.StockExchangeRegister.AsDBValue()));
                lstParams.Add(new SqlParameter("@OccupationType", customer.OccupationType.AsDBValue()));
                lstParams.Add(new SqlParameter("@ResidentArea", customer.ResidentArea.AsDBValue()));
                lstParams.Add(new SqlParameter("@CreatedDate", customer.CreatedDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@CreatedBy", customer.CreatedBy.AsDBValue()));
                lstParams.Add(new SqlParameter("@CreatedCostCenter", customer.CreatedCostCenter.AsDBValue()));
                lstParams.Add(new SqlParameter("@CreatedBranch", customer.CreatedBranch.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedDate", customer.UpdatedDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBy", customer.UpdatedBy.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedCostCenter", customer.UpdatedCostCenter.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBranch", customer.UpdatedBranch.AsDBValue()));

                Utility.ExecuteStoredProc("SPR_CUSTOMER_INSERT", lstParams.ToArray());

                iNum = (int)lstParams[0].Value;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return iNum;
        }

        public int UPDATE(Customer customer)
        {
            int iNum = 0;
            IFRS_Utility Utility = new IFRS_Utility();
            List<SqlParameter> lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@CustomerID", customer.CustomerID.AsDBValue()));
                lstParams.Add(new SqlParameter("@SourceName", customer.SourceName.AsDBValue()));
                lstParams.Add(new SqlParameter("@CustomerCode", customer.CustomerCode.AsDBValue()));
                lstParams.Add(new SqlParameter("@CustomerType", customer.CustomerType.AsDBValue()));
                lstParams.Add(new SqlParameter("@LicenseNo", customer.LicenseNo.AsDBValue()));
                lstParams.Add(new SqlParameter("@PassportNo", customer.PassportNo.AsDBValue()));
                lstParams.Add(new SqlParameter("@IssueAmount", customer.IssueAmount.AsDBValue()));
                lstParams.Add(new SqlParameter("@StockExchangeRegister", customer.StockExchangeRegister.AsDBValue()));
                lstParams.Add(new SqlParameter("@OccupationType", customer.OccupationType.AsDBValue()));
                lstParams.Add(new SqlParameter("@ResidentArea", customer.ResidentArea.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedDate", customer.UpdatedDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBy", customer.UpdatedBy.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedCostCenter", customer.UpdatedCostCenter.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBranch", customer.UpdatedBranch.AsDBValue()));

                iNum = (int)Utility.ExecuteStoredProc("SPR_CUSTOMER_UPDATE", lstParams.ToArray());
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return iNum;
        }

        public int DELETE(Customer customer)
        {
            int iNum = 0;
            IFRS_Utility Utility = new IFRS_Utility();
            List<SqlParameter> lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@CustomerID", customer.CustomerID.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedDate", customer.UpdatedDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBy", customer.UpdatedBy.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedCostCenter", customer.UpdatedCostCenter.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBranch", customer.UpdatedBranch.AsDBValue()));
                iNum = (int)Utility.ExecuteStoredProc("SPR_CUSTOMER_DELETE", lstParams.ToArray());
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return iNum;
        }
    }
}